#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (int i = a; i < b; ++i)
const int N = 1010;
const int dx[] = {-1, 0, 1, 0}, dy[] = {0, -1, 0, 1};
int b[N][N];
int n, m;
typedef pair<int, int> pi;
queue<pi> q;
int main() {
  cin >> n >> m;
  memset(b, -1, sizeof b);
  rep(i, 0, n) {
    string buf;
    cin >> buf;
    rep(j, 0, m) {
      if (buf[j] == '1') {
        q.emplace(i, j);
        b[i][j] = 0;
      }
    }
  }
  while (q.size()) {
    auto [x, y] = q.front();
    q.pop();
    rep(i, 0, 4) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= n || ny < 0 || ny >= m || b[nx][ny] != -1) continue;
      b[nx][ny] = b[x][y] + 1;
      q.emplace(nx, ny);
    }
  }
  rep(i, 0, n) {
    rep(j, 0, m) cout << b[i][j] << ' ';
    cout << endl;
  }
}
